"""
Case Type   : 共享存储-工具-pg_controldata
Case Name   : pg_controldata工具异常操作
Create At   : 2023/4/11
Owner       : opentestcase019
Description :
    1.pg_controldata工具异常操作
    2.pg_controldata工具-I指定不存在的节点
    3.pg_controldata工具不指定--enable-dss
    4.pg_controldata工具指定错误的--socketpath
    5.pg_controldata工具指定不存在的DATADIR路径
Expect      :
    1.合理报错
    2.显示结果为0
    3.显示成功
    4.合理报错
    5.合理报错
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common


class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.common = Common()

    def test_sharestorage_tool(self):
        step1 = "-----step1:pg_controldata工具异常操作;expect:合理报错-----"
        self.logger.info(step1)
        cmd_list = ['-v', '-version', '-h', '-H']
        for cmd in cmd_list:
            execute_cmd = f'''source {macro.DB_ENV_PATH};pg_controldata {cmd}'''
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            self.assertIn("pg_controldata: invalid option",
                          execute_msg, "执行失败" + step1)

        step2 = "-----step2.pg_controldata工具-I指定不存在的节点;expect:显示结果为0-----"
        self.logger.info(step2)
        cmd_list = ['63', '64']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};" \
                f"pg_controldata --enable-dss " \
                f"--socketpath={macro.UDS_PATH} +data -I {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            if cmd == '63':
                self.assertIn("pg_control data (instance id 63)",
                              execute_msg, "执行失败" + step2)
            else:
                self.assertIn("reformer data (last page id 64)",
                              execute_msg, "执行失败" + step2)

        step3 = "-----step3.pg_controldata工具不指定--enable-dss;expect:显示成功-----"
        self.logger.info(step3)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"pg_controldata --socketpath={macro.UDS_PATH} +data -I 0"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("pg_control data (instance id 0)",
                      execute_msg, "执行失败" + step3)

        step4 = "-----step4.pg_controldata工具指定错误的--socketpath;expect:合理报错-----"
        self.logger.info(step4)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"pg_controldata --enable-dss " \
            f"--socketpath={macro.UDS_PATH}_error +data -I 0"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('pg_controldata: could not open file '
                      '"+data/pg_control" for reading: Unknown error',
                      execute_msg, "执行失败" + step4)

        step5 = "-----step5.pg_controldata工具指定不存在的DATADIR路径;expect:合理报错-----"
        self.logger.info(step5)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"pg_controldata --enable-dss " \
            f"--socketpath={macro.UDS_PATH} +data_not_exists -I 0"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('pg_controldata: could not open file '
                      '"+data_not_exists/pg_control" for reading: '
                      'Unknown error 2040', execute_msg, "执行失败" + step5)

    def tearDown(self):
        self.logger.info("-----无需环境清理-----")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
